package cn.basic.util;

import java.util.Objects;

/**
 *  @author: zzz
 *  @date: 2020/5/18 11:16
 */
public class StringUtils {
	public static String stringArrayToString(String[] args) {
		String res = "";
		for (int i = 0; i < args.length; i++) {
			if (i == args.length - 1) {
				res = res + args[i];
			} else {
				res = res + args[i] + ",";
			}
		}
		return res;
	}

	public static String remove(String resource, char ch) {
		StringBuffer buffer = new StringBuffer();
		int position = 0;
		char currentChar;

		while (position < resource.length()) {
			currentChar = resource.charAt(position++);
			// 如果当前字符不是要去除的字符，则将当前字符加入到StringBuffer中
			if (currentChar != ch)
				buffer.append(currentChar);
		}
		return buffer.toString();
	}
	/**
	 * 检测是否为空格串或null引用、空字符串.
	 * @param str
	 * @return 是返回true; 否则返回false
	 */
	public static boolean isBlank(String str) {
		if (isEmpty(str)) {
			return true;
		}

		int strLen = str.length();
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}

		return true;
	}

	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		}
		return false;
	}

	public static boolean notEmpty(String str) {
		return !isEmpty(str);
	}

	public static String addZeroForNum(int num, int strLength) {
		String str = String.valueOf(num);
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(str);// 左补0
				str = sb.toString();
				strLen = str.length();
			}
		}
		return str;
	}

	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
			'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z' };

	/**
	 * 将十进制的数字转换为指定进制的字符串。
	 *
	 * @param i
	 *            十进制的数字。
	 * @param system
	 *            指定的进制，常见的2/8/16。
	 * @return 转换后的字符串。
	 */
	public static String numericToString(int i, int system) {
		long num = 0;
		if (i < 0) {
			num = ((long) 2 * 0x7fffffff) + i + 2;
		} else {
			num = i;
		}
		char[] buf = new char[32];
		int charPos = 32;
		while ((num / system) > 0) {
			buf[--charPos] = digits[(int) (num % system)];
			num /= system;
		}
		buf[--charPos] = digits[(int) (num % system)];
		return new String(buf, charPos, (32 - charPos));
	}

	/**
	 * 将其它进制的数字（字符串形式）转换为十进制的数字。
	 *
	 * @param s
	 *            其它进制的数字（字符串形式）
	 * @param system
	 *            指定的进制，常见的2/8/16。
	 * @return 转换后的数字。
	 */
	public static int stringToNumeric(String s, int system) {
		char[] buf = new char[s.length()];
		s.getChars(0, s.length(), buf, 0);
		long num = 0;
		for (int i = 0; i < buf.length; i++) {
			for (int j = 0; j < digits.length; j++) {
				if (digits[j] == buf[i]) {
					num += j * Math.pow(system, buf.length - i - 1);
					break;
				}
			}
		}
		return (int) num;
	}

	// 通过17位UkeyID解析出12位字符串，充当受理单位ID
	public static String stringToString(String s) {

		// System.out.println("17位UkeyID："+s);
		String s1 = s.substring(0, 8);
		String s2 = s.substring(8, 17);
		s1 = numericToString(Integer.parseInt(s1), 32);
		s2 = numericToString(Integer.parseInt(s2), 32);
		if (s1.length() < 6) {
			int j = 6 - s1.length();
			for (int i = 0; i < j; i++) {
				s1 = "0" + s1;
			}
		}
		if (s2.length() < 6) {
			int j = 6 - s2.length();
			for (int i = 0; i < j; i++) {
				s2 = "0" + s2;
			}
		}
		// System.out.println("12位字符串： " + s1 + s2);
		return s1 + s2;
	}

	// 通过12位字符串，反解析出17位UkeyID
	public static String stringToString1(String s) {

		// System.out.println("12位字符串："+s);
		String s4 = s.substring(0, 6);
		String s5 = s.substring(6, 12);
		s4 = s4.replaceFirst("^0*", "");
		s5 = s5.replaceFirst("^0*", "");
		s4 = stringToNumeric(s4, 32) + "";
		s5 = stringToNumeric(s5, 32) + "";
		if (s5.length() < 9) {
			int j = 9 - s5.length();
			for (int i = 0; i < j; i++) {
				s5 = "0" + s5;
			}
		}
		return s4 + s5;
	}

	/**
	 * arrayStr中是否包含str
	 *
	 * @param arrayStr
	 *            西文逗号分隔 如 a,b,c
	 * @param str
	 *            a
	 * @return
	 */
	public static boolean isContainsString(String arrayStr, String str) {
		String[] array = arrayStr.split(",");
		boolean isContains = false;
		for (String temp : array) {
			if (Objects.equals(str, temp)) {
				isContains = true;
				break;
			}
		}
		return isContains;
	}

    /**
	 * Convert byte[] to hex string.这里我们可以将byte转换成int，然后利用Integer.toHexString(int)来转换成16进制字符串。
	 * @param src byte[] data
	 * @return hex string
	 */
	public static String bytesToHexString(byte[] src){
	    StringBuilder stringBuilder = new StringBuilder("");
	    if (src == null || src.length <= 0) {
	        return null;
	    }
	    for (int i = 0; i < src.length; i++) {
	        int v = src[i] & 0xFF;
	        String hv = Integer.toHexString(v);
	        if (hv.length() < 2) {
	            stringBuilder.append(0);
	        }
	        stringBuilder.append(hv);
	    }
	    return stringBuilder.toString().toUpperCase();
	}
	/**
	 * Convert hex string to byte[]
	 * @param hexString the hex string
	 * @return byte[]
	 */
	public static byte[] hexStringToBytes(String hexString) {
	    if (hexString == null || hexString.equals("")) {
	        return null;
	    }
	    hexString = hexString.toUpperCase();
	    int length = hexString.length() / 2;
	    char[] hexChars = hexString.toCharArray();
	    byte[] d = new byte[length];
	    for (int i = 0; i < length; i++) {
	        int pos = i * 2;
	        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
	    }
	    return d;
	}
	/**
	 * Convert char to byte
	 * @param c char
	 * @return byte
	 */
	 private static byte charToByte(char c) {
	    return (byte) "0123456789ABCDEF".indexOf(c);
	}
	//将指定byte数组以16进制的形式打印到控制台
	public static void printHexString( byte[] b) {
	   for (int i = 0; i < b.length; i++) {
	     String hex = Integer.toHexString(b[i] & 0xFF);
	     if (hex.length() == 1) {
	       hex = '0' + hex;
	     }
	     System.out.print(hex.toUpperCase() );
	   }
	}



    /**
     * 将字符串数组连接成字符串.
     * <p>
     * 默认使用","连接. 如果字符穿数组中有null，则使用"null".
     *
     * @param strArr
     * @return
     * @see #join(String[], String)
     */
    public static String join(String[] strArr) {
        return join(strArr, ",");
    }

    /**
     * 将字符串数组用指定的分隔符连接成字符串.
     * <p>
     * 如果字符穿数组中有null，则使用"null".
     *
     * @param strArr
     * @param delimeter
     * @return
     */
    public static String join(String[] strArr, String delimeter) {
        if (strArr == null) {
            return null;
        }

        if (strArr.length == 0) {
            return "";
        }

        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < strArr.length; i++) {
            if (i != 0) {
                buf.append(delimeter);
            }
            buf.append(strArr[i]);
        }

        return buf.toString();
    }

    /**
     * 拼接两个字节数组
     * @param src1
     * @param src2
     * @return 拼接后的字节数组
     */
    public static byte[] contratArrays(byte[] src1,byte[] src2){
        if(null == src1 || src1.length == 0){
            return src2;
        }
        if(null == src2 || src2.length == 0){
            return src1;
        }
        byte[] destByte = new byte[src1.length + src2.length];
        System.arraycopy(src1, 0, destByte, 0, src1.length);
        System.arraycopy(src2, 0, destByte, src1.length, src2.length);
        return destByte;
    }

}
